﻿using EagleProject.LinQDataContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using PagedList.Mvc;
using EagleProject.Models;

namespace EagleProject.Controllers
{
    public class AgentController : MyController
    {
       

        //
        // GET: /Agent/Success
        public ActionResult Success()
        {
            return View();
        }

        //
        // GET: /Agent/
        public ActionResult Index()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Agent/");
            if (!IsAgent()) return RedirectToAccessDenied();

            return View();
        }

        //
        // GET: /Agent/RegisterForm/
        public ActionResult RegisterForm()
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Agent/RegisterForm/");
            if (!IsAgent()) return RedirectToAccessDenied();

            ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
            ViewBag.ListLGAs = new SelectList(DatabaseController.GetLGAs(), "ID", "Name");
            ViewBag.ListCountry = new SelectList(DatabaseController.GetCountries(), "ID", "Name");
            ViewBag.ListCard = ListCard;
            ViewBag.ListMarialStatus = ListMarialStatus;

            return View();
        }

        //
        // GET: /Agent/RegisterForm/
        [HttpPost]
        public ActionResult RegisterForm(FormAgentCreateSubscriber model)
        {
            if (!IsAuthenticated()) return RedirectToLogin(@"~/Agent/RegisterForm");
            if (!IsAgent()) return RedirectToAccessDenied();

            int cardType = GetIntParam("CardType");
            if (cardType >= 1 && cardType <= 4)
            {
                DatabaseController.CreateSubscriberRegSubscriberCommonUser(model, cardType, GetUserInformation().ID);

                return RedirectToAction("Success");
            }
            else
            {
                // Information error !
                return View(model);
            }
        }

        //
        // GET: /Agent/ListSubscriber
        public ActionResult ListSubscriber(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value : 1;

            if (!IsAuthenticated()) return RedirectToLogin(@"~/Agent/ListSubscriber/");
            if (!IsAgent()) return RedirectToAccessDenied();

            var data = GetData(currentPageIndex, NumberOfRecordPerPage);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxSubscriberList", data);
            else
                return View(data);
        }

        private MvcPaging.IPagedList<Subcriber> GetData(int currentPageIndex, int numberPerPage)
        {
            var listData = DatabaseController.GetAllSubscriberCreatedByMe(GetUserInformation().ID);
            var subData = MvcPaging.IQueryableExtensions.ToPagedList(listData, currentPageIndex, numberPerPage);

            return subData;
        }
    }
}
